#pragma once
#include <QObject>
#include <QHttpServer>
#include <QProtobufSerializer>
#include <QWebSocketServer>

#include "user.qpb.h"
#include "base.qpb.h"
#include "friend.qpb.h"
#include "message_storage.qpb.h"
#include "message_transmit.qpb.h"
#include "notify.qpb.h"
#include "file.qpb.h"
#include "speech_recognition.qpb.h"

/**********************************************************************************************
 * HttpServer
 **********************************************************************************************/


pbPackage::UserInfo makeUserInfo(int index, const QByteArray &avatar);
pbPackage::MessageInfo makeTextMessageInfo(int index, const QString& chatSessionId,const QByteArray &avatar);
pbPackage::MessageInfo makeImageMessageInfo(int index, const QString& chatSessionId,const QByteArray &avatar);

class HttpServer : public QObject
{
    Q_OBJECT;
public:
    static HttpServer* getInstance();
    //初始化HttpServer
    //可能出错的初始化操作，一般不放在构造函数中
    bool init();
    QHttpServerResponse getUserInfo(const QHttpServerRequest& req);
    QHttpServerResponse getFriendList(const QHttpServerRequest& req);
    QHttpServerResponse getChatSessionList(const QHttpServerRequest& req);
    QHttpServerResponse getApplyList(const QHttpServerRequest& req);
    QHttpServerResponse getRecentMessage(const QHttpServerRequest& req);
    QHttpServerResponse newMessage(const QHttpServerRequest& req);
    QHttpServerResponse setNickName(const QHttpServerRequest& req);
    QHttpServerResponse setDesc(const QHttpServerRequest& req);
    QHttpServerResponse getPhoneVerifyCode(const QHttpServerRequest& req);
    QHttpServerResponse setPhone(const QHttpServerRequest& req);
    QHttpServerResponse setAvatar(const QHttpServerRequest& req);
    QHttpServerResponse removeFriend(const QHttpServerRequest& req);
    QHttpServerResponse applyFriend(const QHttpServerRequest& req);
    QHttpServerResponse addFriendProcess(const QHttpServerRequest& req);
    QHttpServerResponse createChatSession(const QHttpServerRequest& req);
    QHttpServerResponse getMemberList(const QHttpServerRequest& req);
    QHttpServerResponse searchFriend(const QHttpServerRequest& req);
    QHttpServerResponse searchHistoryByKey(const QHttpServerRequest& req);
    QHttpServerResponse searchHistoryByTime(const QHttpServerRequest& req);
    QHttpServerResponse userLogin(const QHttpServerRequest& req);
    QHttpServerResponse userRegister(const QHttpServerRequest& req);
    QHttpServerResponse phoneLogin(const QHttpServerRequest& req);
    QHttpServerResponse phoneRegister(const QHttpServerRequest& req);
    QHttpServerResponse getSingleFile(const QHttpServerRequest& req);
    QHttpServerResponse speechRecognition(const QHttpServerRequest& req);


private:

    static HttpServer* instance;

    QHttpServer httpServer;
    QProtobufSerializer serialize; //可以序列化/反序列化PB的对象
};



/**********************************************************************************************
 * WebSocketServer
 **********************************************************************************************/
class WebSocketServer : public QObject
{
    Q_OBJECT;
private:
    static WebSocketServer* instance;
    QWebSocketServer webSocketServer;

private:
    WebSocketServer();
    int messageIndex = 0;
     QProtobufSerializer serialize; //可以序列化/反序列化PB的对象

public:

    static WebSocketServer* getInstance();
    bool init();
signals:
    void sendTextResp();
    void deleteFriendResp();
    void sendFriendApply();
    void sendAddFriendApply(bool agree);
    void sendCreateChatSession();
    void sendImageMessage();
    void sendFileMessage();
    void sendSpeechMessage();
};
